const requestQueue = {
    namespaced: true,
    state: {
        /**
         * 请求队列，
         * key: 缓存key，value: 返回方法
         */
        queue: {}
    },
    mutations: {
        /**
         * 设置
         * @param state
         * @param data { cacheKey: '缓存标识', resolve: '回调函数'}
         */
        SET_QUEUE(state, data) {
            const { cacheKey } = data
            if (state.queue[cacheKey]) {
                state.queue[cacheKey].push(data)
            } else {
                state.queue[cacheKey] = [data]
            }
        },
        /**
         * 清除缓存key
         * @param state
         * @param data
         * @constructor
         */
        DEL_QUEUE(state, data){
            const { cacheKey } = data
            state.queue[cacheKey] = []
        }
    },
    getters: {
        /**
         * 获取指定key下的队列
         * @param state
         * @return {function(*): *}
         */
        getQueue: (state) =>{
            return (key) => state.queue[key]
        }
    },
    actions: {
        // 设置请求缓存队列
        async setQueue({ commit }, data) {
            commit('SET_QUEUE', data);
        },
        // 清楚请求缓存队列
        async clearQueue({ commit }, data) {
            commit('DEL_QUEUE', data);
        }
    }
}
export default requestQueue;
